import csv
import json
import matplotlib.pyplot as plt
def print_connection(d,category):
    print(category)
    plt.rcParams['font.sans-serif'] = 'SimHei'  # 设置中文显示
    plt.figure(figsize=(6, 6))  # 将画布设定为正方形，则绘制的饼图是正圆
    label=[]
    values=[]
    all=[]
    for key,val in d.items():
        all.append([val,key])
    all.sort()
    all=all[len(all)-10:]
    for x in all:
        label.append(x[1])
        values.append(x[0])
    for data in all:
        print(data[1],data[0],"|",end=' ')
    plt.pie(values,autopct='%1.1f%%',labels=label)  # 绘制饼图
    plt.title(category)  # 绘制标题
    plt.show()
    print()

filename=r"./data/m-a_edges.csv"
all_mashup_api=set()#mashup中使用的全部api
all_api=set()#全部api
used_cnt=0
with open(filename,encoding="utf-8") as f:
    reader = csv.reader(f)
    for i,row in enumerate(reader):
        row=row[-1].split("\t")
        #print(i,row)
        if(row[-1][0]!='/'):
            continue
        all_mashup_api.add(row[-1])
        used_cnt+=1
print("mashup中包含web api的个数：",len(all_mashup_api))
print("web api被使用的次数：",used_cnt)

filename=r"./data/api_nodes_estimator.csv"
with open(filename,encoding="utf-8") as f:
    reader = csv.reader(f)
    for i,row in enumerate(reader):
        row=row[0].split("\t")
        if(row[0]!="api"):
            continue
        if(row[1][0]=='/'):
            all_api.add(row[1])
print("提供商发布web api个数：",len(all_api))
print("---------------------------------")

all_data=[]#所有json数据
categoriesDict={}#每个种类相关的信息
file_pathes=["./data/raw/api_mashup/active_mashups_data.txt","./data/raw/api_mashup/deadpool_mashups_data.txt"]
for file_path in file_pathes:
    f = open(file_path, encoding="utf-8")
    all_data += json.load(f)
for i,data in enumerate(all_data):
    if data==None:
        continue
    apis=data["related_apis"]
    urls=[]#当前mashup关联的url
    for api in apis:
        if api!=None:
            urls.append(api["url"])
    #print(urls)
    for category in data["categories"]:
        for url in urls:
            if category not in categoriesDict:
                categoriesDict[category]={}
            categoriesDict[category][url] = categoriesDict[category].get(url,0)+1
#print(categoriesDict)
print_connection(categoriesDict["Search"],"Search")
print_connection(categoriesDict["Mapping"],"Mapping")
print("---------------------------------")

all_data=[]
tagsDict={}
file_pathes=["./data/raw/api_mashup/active_apis_data.txt","./data/raw/api_mashup/deadpool_apis_data.txt"]
for file_path in file_pathes:
    f = open(file_path, encoding="utf-8")
    all_data += json.load(f)

for i,data in enumerate(all_data):
    if data==None:
        continue
    #print(data)
    tags=data["tags"]
    if len(data['versions'])==0:
        continue
    style=data['versions'][0]['style']
    for tag in tags:
        if tag not in tagsDict:
            tagsDict[tag]={}
        tagsDict[tag][style]=tagsDict[tag].get(style,0)+1
print('eCommerce')
print(tagsDict['eCommerce'])
print('Marketplace')
print(tagsDict['Marketplace'])
